const WIDTH = 640;
const HEIGHT = 480;
var lvlObjectNames = []; // Array containing names of every objects in the level
var lvlObjects = []; // Associative array with String keys (object names) and MutableObject values.
const PAST = 1;
const PRESENT = 2;
const FUTURE = 3;
const FOOTER_DIM = [0, HEIGHT-70, WIDTH, 70];
var currentLevel = 1;
const LEVELMAX = 5;
var currentTime = PRESENT;
var timeArray = {};
timeArray[PAST]='past';
timeArray[PRESENT]='present';
timeArray[FUTURE]='future';
var mainPlayer = 0;
var isTimeTravelling = false;
var isChangingLevel = false;
var soundPaused = false;
var posX = 0;
var posY = 0;
var flip = false;
var itemStocked = 0;

function MutableObject(entity, state) {
	this.obj = entity;
	this.objState = state;
	this.collected = false; // if 1,2 or 3, object is not in the level anymore, at any time
}

MutableObject.prototype.makeState = function(state) {
	return this.makeState(state, null);
}
MutableObject.prototype.makeState = function(state, playerObj) {
	// alert(" old state : " + this.objState + "\nnew state : " + state);
	state = Math.max(1, Math.min(3, state));
	var settings = new Object();
	settings.name = this.obj.name;
	settings.num = this.obj.num;
	settings.mutobjType = this.obj.mutobjType;
	// var newPosX = this.obj.pos.x + this.obj.width - 32;
	// var newPosY = this.obj.pos.y + this.obj.height - 32;
	if (playerObj != null) {
		// alert("initObj putobj " + playerObj.collisionBox.colPos.x);
		var diffFlipX =0;
		if (!flip) diffFlipX = playerObj.collisionBox.width;
		var newPosX = 32*~~((playerObj.pos.x + playerObj.collisionBox.colPos.x + diffFlipX)/32);
		var newPosY = 32*~~((playerObj.pos.y + playerObj.height)/32) - 32;//+ playerObj.height - 32;
		// console.log('playerHeight :' + playerObj.height);
		// alert("player put : " + newPosX + "`y : " + newPosY);
	} else {
		var newPosX = this.obj.pos.x + this.obj.width - 32;
		var newPosY = this.obj.pos.y + this.obj.height - 32;
	}
	switch (state) {
		case 1 :
			settings.collidable = true;
			settings.image = "plant_1";
			break;
		case 2 :
			settings.collidable = false;
			settings.image = "plant_2";
			for (var i=1 ; i<=2 ; i++) {
				me.game.collisionMap.setTile(newPosX/32, newPosY/32 - i, 145);
			}
			break;
		case 3 :
			settings.collidable = false;
			settings.image = "plant_3";
			// alert("width : " + this.image.width);
			// newPosX-= this.image/2;
			for (var i=1 ; i<=6 ; i++) {
				me.game.collisionMap.setTile(newPosX/32, newPosY/32 - i, 145);
			}
			break;
	}
	// settings.spritewidth = 32;

	// alert("make newMutObj x : " + newPosX + " y : " + newPosY);
	var z = this.obj.z;
	var newMutObj = new Seed(newPosX, newPosY, settings);
	this.collected = false;
	me.game.add(newMutObj, z);
	me.game.sort();
	// mainPlayer.setAnimationFrame(1);
	// mainPlayer.updateMovement();
}
